package com.lmy.springboot_rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * rabbitmq配置类
 * 用于配置交换机、队列及其绑定关系等
 */
@Configuration
public class FanoutRabbitmqConfiguration {

    /**========================配置交换机========================*/
    @Bean
    public FanoutExchange fanoutExchange() {
        //配置交换机 fanout_order_exchange(分发模式为fanout)，持久化且不自动删除
        return new FanoutExchange("fanout_order_exchange", true, false);
    }

    /**========================配置队列========================*/
    @Bean
    public Queue sms_fanout_queue() {
        //配置队列 sms_fanout_queue，持久化
        return new Queue("sms_fanout_queue", true);
    }

    @Bean
    public Queue email_fanout_queue() {
        //配置队列 email_fanout_queue，持久化
        return new Queue("email_fanout_queue", true);
    }

    @Bean
    public Queue message_fanout_queue() {
        //配置队列 message_fanout_queue，持久化
        return new Queue("message_fanout_queue", true);
    }

    /**========================配置交换机和队列的绑定关系========================*/
    @Bean
    public Binding smsFanoutBinding() {
        //将队列 sms_fanout_queue 绑定到 交换机fanout_order_exchange
        return BindingBuilder.bind(sms_fanout_queue()).to(fanoutExchange());
    }

    @Bean
    public Binding emailFanoutBinding() {
        //将队列 email_fanout_queue 绑定到 交换机fanout_order_exchange
        return BindingBuilder.bind(email_fanout_queue()).to(fanoutExchange());
    }

    @Bean
    public Binding messageFanoutBinding() {
        //将队列 message_fanout_queue 绑定到 交换机fanout_order_exchange
        return BindingBuilder.bind(message_fanout_queue()).to(fanoutExchange());
    }

}
